package com.huitone.smspfm.socket.protocol.sgip.conversion;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.socket.protocol.sgip.constant.CommandID;
import com.huitone.smspfm.socket.protocol.sgip.constant.Constant;

/**   
* @Title FormatConverter.java 
* @Package com.huitone.smspfm.socket.protocol.sgip.conversion 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年3月23日 下午4:55:00 
* @version V1.0   
*/
public class TransferConverter {

	/**
	 * 网络字节序转换(int to 大端字节序) (长度4字节)
	 * @param i
	 * @return
	 */
	public static byte[] toBE4(int i) {
		return new byte[] {
			(byte) ((i >> 24) & 0xFF),  
	        (byte) ((i >> 16) & 0xFF),     
	        (byte) ((i >> 8) & 0xFF),     
	        (byte) (i & 0xFF)  
		};
	}
	
	public static byte[] toBE4(long i) {
		return new byte[] {
			(byte) ((i >> 24) & 0xFF),  
	        (byte) ((i >> 16) & 0xFF),     
	        (byte) ((i >> 8) & 0xFF),     
	        (byte) (i & 0xFF)  
		};
	}
	
	public static byte[] toBE1(int i) {
		return new byte[] {
	        (byte)i 
		};
	}
	
	public static int bE2Int4(byte[] b) {
		return   b[3] & 0xFF |  
	            (b[2] & 0xFF) << 8 |  
	            (b[1] & 0xFF) << 16 |  
	            (b[0] & 0xFF) << 24;
	}
	
	public static int bE2Int1(byte[] b) {
		return  b[0];
	}
	
	/**
	 * byte数组转long 
	 * @param b
	 * @param blength > 0
	 * @return
	 */
	public static long bE2Long(byte[] b, int blength) {
		
		  long s = 0;  
		  for (int i = 0; i < blength - 1; i++) {  
		    if (b[i] >= 0) {  
		    s = s + b[i];  
		    } else {  
		    s = s + 256 + b[i];  
		    }  
		    s = s * 256;  
		  }  
		  if (b[blength - 1] >= 0) {  
		    s = s + b[blength - 1];  
		  } else {  
		    s = s + 256 + b[blength - 1];  
		  }  
		  return s;  
	}
	
	/**
	 * 定长字符串，左对齐存储，空余位置补’\0’
	 * @param b
	 * @param s
	 * @return
	 * @throws  
	 */
	public static byte[] toLJ(byte[] b, String s) {
		
		byte[] bytes;
		try {
			bytes = s.getBytes(Constant.defaultEncoding);
			for (int i = 0 ; i < b.length ; i ++) {
				if (i < bytes.length) {
					b[i] = bytes[i];
				} else {
					b[i] = 0;
				}
			}
		} catch (UnsupportedEncodingException e) {
			bytes = s.getBytes();
		}
		
		return b;
	}
	
	/**
	 * 将定长字符串byte转成字符串,去除了多余的\0
	 * @param b
	 * @return
	 */
	public static String lj2Str(byte[] b) {
		
		int index = 0;
		
		if (b != null && b.length > 0) {
			for(int i = b.length - 1 ; i > 0 ; i --) {
				if (b[i] != 0) {
					index = i ; break;
				}
			}
			try {
				return StringUtils.trim(new String(b, 0, index + 1, Constant.defaultEncoding));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static String stringToAscii(String value) {  
	    StringBuffer sbu = new StringBuffer();  
	    char[] chars = value.toCharArray();   
	    for (int i = 0; i < chars.length; i++) {  
	        if(i != chars.length - 1)  
	        {  
	            sbu.append((int)chars[i]).append(",");  
	        }  
	        else {  
	            sbu.append((int)chars[i]);  
	        }  
	    }  
	    return sbu.toString();  
	}
	
	public static String asciiToString(String value) {  
	    StringBuffer sbu = new StringBuffer();  
	    String[] chars = value.split(",");  
	    for (int i = 0; i < chars.length; i++) {  
	        sbu.append((char) Integer.parseInt(chars[i]));  
	    }  
	    return sbu.toString();  
	}  
	
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
		
		System.out.println(bE2Long(toBE4(CommandID.SGIP_REPORT_RESP), 4));
		
		System.out.println(bE2Int1(toBE1(-128)));
		
		System.out.println(((byte)1));
		
		System.out.println(new byte[] {1});
		
	}
}
